Completed
Push — master ( 605e3d...3de0bb )
by Muhammad Dyas
14s queued 13s
created

NewPollFormCard.buildCloseConfigSection   B

Complexity

Conditions 3

Size

Total Lines 47
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 37
dl 0
loc 47
rs 8.9919
c 0
b 0
f 0
cc 3
1
import BaseCard from './BaseCard';
2
import {ClosableType, LocaleTimezone, PollForm} from '../helpers/interfaces';
3
import {MAX_NUM_OF_OPTIONS} from '../config/default';
4
import {chat_v1 as chatV1} from 'googleapis/build/src/apis/chat/v1';
5
import {offsetToTimezone} from '../helpers/time';
6
7
export default class NewPollFormCard extends BaseCard {
8
  private config: PollForm;
9
  private timezone: LocaleTimezone;
10
11
  constructor(config: PollForm, timezone: LocaleTimezone) {
12
    super();
13
    this.config = config;
14
    this.timezone = timezone;
15
  }
16
17
  create() {
18
    this.buildSections();
19
    this.buildFooter();
20
    return this.card;
21
  }
22
23
  buildSections() {
24
    this.buildTopicInputSection();
25
    this.buildOptionSwitchSection();
26
    this.buildCloseConfigSection();
27
    if (this.config.autoClose) {
28
      this.buildAutoCloseSection();
29
    }
30
  }
31
32
  buildTopicInputSection() {
33
    const widgets = [];
34
    widgets.push(this.buildHelpText());
35
    widgets.push(this.topicInput(this.config.topic));
36
    for (let i = 0; i < MAX_NUM_OF_OPTIONS; ++i) {
37
      const choice = this.config.choices?.[i];
38
      widgets.push(this.optionInput(i, choice));
39
    }
40
    this.card.sections!.push({
41
      'collapsible': true,
42
      'uncollapsibleWidgetsCount': 6,
43
      widgets,
44
    });
45
  }
46
47
  buildOptionSwitchSection() {
48
    this.card.sections!.push({
49
      'widgets': [
50
        {
51
          'decoratedText': {
52
            'bottomLabel': 'If this checked the voters name will be not shown',
53
            'text': 'Anonymous voter',
54
            'switchControl': {
55
              'controlType': 'SWITCH',
56
              'name': 'is_anonymous',
57
              'value': '1',
58
              'selected': this.config.anon ?? false,
59
            },
60
          },
61
          'horizontalAlignment': 'CENTER',
62
        },
63
        {
64
          'decoratedText': {
65
            'bottomLabel': 'After the poll is created, other member can add more option',
66
            'text': 'Allow to add more option(s)',
67
            'switchControl': {
68
              'controlType': 'SWITCH',
69
              'name': 'allow_add_option',
70
              'value': '1',
71
              'selected': this.config.optionable ?? true,
72
            },
73
          },
74
          'horizontalAlignment': 'CENTER',
75
        },
76
      ],
77
    });
78
  }
79
80
  buildCloseConfigSection() {
81
    const widgets: chatV1.Schema$GoogleAppsCardV1Widget[] = [
82
      {
83
        'selectionInput': {
84
          'type': 'DROPDOWN',
85
          'label': 'Allow to manually close poll',
86
          'name': 'type',
87
          'items': [
88
            {
89
              'text': 'Yes, but only creator',
90
              'value': '1',
91
              'selected': this.config.type === ClosableType.CLOSEABLE_BY_CREATOR,
92
            },
93
            {
94
              'text': 'Yes, anyone can close',
95
              'value': '2',
96
              'selected': this.config.type === ClosableType.CLOSEABLE_BY_ANYONE,
97
            },
98
            {
99
              'text': 'No, I want unclosable poll',
100
              'value': '0',
101
              'selected': this.config.type === ClosableType.UNCLOSEABLE,
102
            },
103
          ],
104
        },
105
        'horizontalAlignment': 'START',
106
      },
107
      {
108
        'decoratedText': {
109
          'topLabel': '',
110
          'text': 'Automatic close poll at certain time',
111
          'bottomLabel': 'The schedule time will show up',
112
          'switchControl': {
113
            'controlType': 'SWITCH',
114
            'name': 'is_autoclose',
115
            'value': '1',
116
            'selected': this.config.autoClose ?? false,
117
            'onChangeAction': {
118
              'function': 'new_poll_on_change',
119
              'parameters': [],
120
            },
121
          },
122
        },
123
      }];
124
    this.card.sections!.push({
125
      widgets,
126
    });
127
  }
128
129
  buildAutoCloseSection() {
130
    const widgets: chatV1.Schema$GoogleAppsCardV1Widget[] = [];
131
    const timezone = offsetToTimezone(this.timezone.offset);
132
    const nowMs = Date.now() + this.timezone.offset + 18000000;
133
    widgets.push(
134
      {
135
        'dateTimePicker': {
136
          'label': 'Close schedule time ' + timezone,
137
          'name': 'close_schedule_time',
138
          'type': 'DATE_AND_TIME',
139
          'valueMsEpoch': nowMs.toString(),
140
        },
141
      });
142
143
    widgets.push(
144
      {
145
        'decoratedText': {
146
          'text': 'Auto mention <b>@all</b> on 5 minutes before poll closed',
147
          'bottomLabel': 'This is to prevent other users to vote before the poll is closed',
148
          'switchControl': {
149
            'controlType': 'SWITCH',
150
            'name': 'auto_mention',
151
            'value': '1',
152
            'selected': this.config.autoMention ?? false,
153
          },
154
        },
155
      });
156
    this.card.sections!.push({
157
      widgets,
158
    });
159
  }
160
161
  buildHelpText() {
162
    return {
163
      textParagraph: {
164
        text: 'Enter the poll topic and up to 10 choices in the poll. Blank options will be omitted.<br>' +
165
          'For scheduled auto close, the minimum time is 5 minutes after poll created.',
166
      },
167
    };
168
  }
169
170
  topicInput(topic: string) {
171
    return {
172
      textInput: {
173
        label: 'Topic',
174
        type: 'MULTIPLE_LINE',
175
        name: 'topic',
176
        value: topic,
177
      },
178
    };
179
  }
180
181
  optionInput(
182
    index: number, value: string): chatV1.Schema$GoogleAppsCardV1Widget {
183
    return {
184
      textInput: {
185
        label: `Option ${index + 1}`,
186
        type: 'SINGLE_LINE',
187
        name: `option${index}`,
188
        value: value || '',
189
      },
190
    };
191
  }
192
193
  buildFooter() {
194
    this.card.fixedFooter = {
195
      'primaryButton': this.createButton('Submit', 'start_poll'),
196
    };
197
  }
198
}
199